<?PHP

require_once("ESPNScraper.php");

/*   Main class for the user to interface with xmlsports. */
class XMLSports {

    var $sportsmlStr;
    var $outputFile;
    var $CurrWeek = "";
    var $League = "";
    var $Season = "";

    function XMLSports()
    {
    
    }

    function getSportsML() {
        return $this->sportsmlStr;
    }

    function generate()
    {
        #echo "scraping...<br/>";
		$s = new ESPNScraper();
		$s->SetCurrWeek($this->CurrWeek);
		$s->SetLeague($this->League);
		$s->SetSeasonYear($this->Season);
        $this->sportsmlStr = $s->scrape();
        
        // Let's make sure the file exists and is writable first.
		if (is_writable($this->outputFile))
		{
		
		   // In our example we're opening $outputFile in write mode.
		   // The file pointer is at the bottom of the file hence
		   // that's where $sportsmlStr will go when we fwrite() it.
		   if (!$handle = fopen($this->outputFile, 'w+')) {
				 echo "Cannot open file ($this->outputFile)";
				 exit;
		   }
		
		   // Write $sportsmlStr to our opened file.
		   if (fwrite($handle, $this->sportsmlStr) === FALSE) {
			   echo "Cannot write to file ($this->outputFile)";
			   exit;
		   }
		  
		  #echo "writing xml... to file ($this->outputFile)<br/>";
		  
			fclose($handle);
			#echo "$this->outputFile is done.<br/>";
		
		}
		else
		{
			if (!$handle = fopen($this->outputFile, 'x+'))
			{
				echo "The file $this->outputFile is not writable<br/>";
			}
			else
			{
				// Write $sportsmlStr to our opened file.
				if (fwrite($handle, $this->sportsmlStr) === FALSE) {
				   echo "Cannot write to file ($this->outputFile)";
				   exit;
				}
				
				#echo "writing xml... to file ($this->outputFile)<br/>";
				
				fclose($handle);
				#echo "$this->outputFile is done.<br/>";
			}
		}
    }

    function setOutputFile($s)
    {
        $this->outputFile = $s;
    }
    
    function SetWeek($newWeek)
    {
		$this->CurrWeek = $newWeek;
    }
    
    function SetLeague($League)
    {
		$this->League = $League;
    }
	
	function SetSeason($Season)
    {
		$this->Season = $Season;
    }

}

?>

